window.$config = function () {
    return {
        init () {
            console.log('项目运行成功！')
        },
        iswx(){
            var ua = window.navigator.userAgent.toLowerCase();
            if(ua.match(/MicroMessenger/i) == 'micromessenger'){
                return true;
            }else{
                return false;
            }
        },
        wxshare (wxconfig){
            var config = wxconfig,
                title = "",
                summary = "",
                link = "",
                thumb = '',
                content = {
                    title: title,
                    desc:summary,
                    link: link,
                    imgUrl:thumb,
                    type: '',
                    dataUrl: '',
                    success: function () {},
                    cancel: function () {}
                }

            wx.config({
                debug: false,
                appId: config.appId,
                timestamp: config.timestamp,
                nonceStr: config.nonceStr,
                signature: config.signature,
                jsApiList: [
                    'onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ','onMenuShareWeibo'
                ]
            });
            wx.checkJsApi({
                jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ','onMenuShareWeibo'],
                success: function(res) {}
            });
            wx.ready(function () {
                wx.onMenuShareAppMessage(content);
                wx.onMenuShareTimeline(content);
                wx.onMenuShareQQ(content);
                wx.onMenuShareWeibo(content);
            });
        }
    }
}

$(function(){
    // 导航栏移动端时点击显示
    $('.navicon').click(function(){
        $('.menu').toggleClass('on');
    });

    // 导航栏滚动时切换样式
    window.onscroll = function(){
        var top = document.body.scrollTop;
        if(top > 0){
            $('.nav').addClass('on');
        }else{
            $('.nav').removeClass('on');
        }
    };

    // 回到顶部
    $('#toTop').on('click', function(){
        $('html, body').animate({scrollTop: 0},400);
    });

    var s_top,curhei;
    setTimeout(function(){
        s_top = $(window).scrollTop();
        curhei = s_top;  //首次进入时滚动条位置

        $('.content').each(function(){
            $(this).attr('tops',parseInt($(this).offset().top));    //获取各版块位置

            var tarhei = $(this).offset().top-$(this).prev().height();
            if(curhei >= tarhei){   //确定当前版块
                var ind = $(this).index();
                $('.content').attr('class','content');
                $(this).prev().addClass('prev');
                $(this).next().addClass('next');
                $('.descSlide-nav a').eq(ind).addClass('on').siblings().removeClass('on');
            }else if(curhei < $('.content').eq(0).offset().top){
                $('.content').eq(0).next().addClass('next');
                $('.descSlide-nav a').eq(0).addClass('on').siblings().removeClass('on');
            }
        });
    },500);

    var pn = location.pathname;
    var curRouter = pn.substring(1, pn.indexOf('.'));

    if(curRouter=='about'||curRouter=='business'||curRouter=='duty'||curRouter=='video'||curRouter=='vdetial'){
        $('.nav').addClass('list');
        if(curRouter=='about') $('.nav li').eq(1).addClass('on');
        if(curRouter=='business') $('.nav li').eq(2).addClass('on');
        if(curRouter=='video'||curRouter=='vdetial') $('.nav li').eq(3).addClass('on');
        if(curRouter=='duty') $('.nav li').eq(4).addClass('on');
    }else{
        $('.nav li').eq(0).addClass('on');
    }

    var ha = location.hash;
    if(ha == '#1'){
        var image = new Image();
        image.src = '/src/img/tongji.jpg';
    }else if(ha == '#2'){
        var image = new Image();
        image.src = '/src/img/tongji2.jpg';
    }

    //滚动时切换导航
    var scrolldata = 0;
    $(window).scroll(function(){
        var s_top = $(this).scrollTop();
        var curhei = s_top;  //滚动条当前位置

        if(s_top > scrolldata){ //向下滚
            var _this = $('.next');
            var tarhei = parseInt(_this.attr('tops'))-(_this.prev().height()/2); //目标位置

            if(curhei > tarhei){
                var ind = _this.index();
                $('.descSlide-nav a').eq(ind).addClass('on').siblings().removeClass('on');
                $('.content').attr('class','content');
                _this.prev().addClass('prev');
                _this.next().addClass('next');
            }
        }else{  //向上滚
            var _this = $('.prev');
            var tarhei = parseInt(_this.attr('tops'))+parseInt(_this.height()/3); //目标位置

            if(curhei < tarhei){
                var ind = _this.index();
                $('.descSlide-nav a').eq(ind).addClass('on').siblings().removeClass('on');
                $('.content').attr('class','content');
                _this.prev().addClass('prev');
                _this.next().addClass('next');
            }
        }
        scrolldata = s_top;
    });

    //点击右侧导航，移动到相应版块
    $('.descSlide-nav a').click(function(){
        var $index = $(this).index(),
            hash = $(this).data('hash');
        var scrollT = parseInt($('.content').eq($index).attr('tops'))-parseInt($('.nav').height())-50;
        $('.content').attr('class','content');
        $('.content').eq($index).prev().addClass('prev');
        $('.content').eq($index).next().addClass('next');
        $(this).addClass('on').siblings().removeClass('on');
        location.hash = hash;

        $('body').animate({
            scrollTop : scrollT
        },500);
    });
});

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

